<?php

/**
 * Description of topic
 *
 * @author b4by
 */
class page extends MY_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('model_page');
		$this->load->model('model_catalogue');
		$this->load->model('model_page_catalogue');
		$this->load->model('model_file');
		$this->_init();
	}

	public function index() {
		$this->data['temp'] = 'admin/page/index';
		$this->load->view('admin/main', $this->data);
	}

	public function read() {
		$pagination = pagination_init($this->model_page->get_total(), request('p', 1));
		$pages = $this->model_page->get_list(array(
			'limit' => array($pagination->from, $pagination->limit),
		));
		foreach ($pages as &$page) {
			$page->link = url_create('site', 'ViewPage', 'index', $page->id, $page->title);
			$page->edit_link = url_create('admin', 'page', 'update', $page->id);
			$page->registed_count_link = url_create('admin', 'page', 'registed', $page->id);
		}
		echo json_encode(array(
			'state' => 'success',
			'data' => $pages,
			'pagination' => $pagination,
		));
	}

	public function registed($id)
	{
		$page = $this->model_page->get_info($id);
		$this->data['page'] = $page;
		$this->data['temp'] = 'admin/page/registed';
		$this->load->view('admin/main',$this->data);
	}
	
	public function read_registed($id)
	{
		$this->load->model('model_page_user');
		$pagination = pagination_init($this->model_page_user->get_total(array(
			'page_id'	=> $id,
		)), request('p', 1));
		$users = $this->model_page_user->get_registed_users(array(
				'select'	=> 'u.*',
				'where'	=> array('page_id'	=> $id),
				'limit'	=> array($pagination->from, $pagination->limit),
			));
		foreach($users as &$user)
		{
			$user->info_link = url_create('site','account','info',$user->id);
		}
		echo json_encode(array(
			'state'	=> 'success',
			'pagination'	=> $pagination,
			'data'	=> $users,
		));
	}
	
	public function create() {


		if ($this->input->post('submit')) {

			$this->load->helper('form');
			$this->load->library('form_validation');

			$this->form_validation->set_rules('title', 'lang:page_title', 'required');
			$this->form_validation->set_rules('content', 'lang:page_content', 'required');
			$this->form_validation->set_rules('catalogues', 'lang:catalogues', 'required');

			$data = request_filter(array('title', 'content', 'summary', 'image_id', 'home', 'main', 'product'));
			if ($this->form_is_valid()) {

				$this->model_file->update($data['image_id'], array('status' => 1));
				$img = $this->model_file->get_info($data['image_id']);
				$data['image'] = $img->file_name;
				$data['thumb'] = $img->thumb_name;
				$data['time'] = now();
				if ($data['main'] == 1)
					$this->model_page->update_rule(array(), array('main' => 0));
				$page_id = $this->model_page->create($data);

				//catalogues
				$catalogues = request('catalogues');
				foreach ($catalogues as $cat_id) {
					$this->model_page_catalogue->create(array(
						'page_id' => $page_id,
						'cat_id' => $cat_id
					));
				}
				echo json_encode(array(
					'state' => 'success',
					'redirect' => url_create('admin', 'page', 'index')
				));
			} else {
				echo json_encode(array(
					'state' => 'error',
					'errors' => $this->data['errors']
				));
			}
			exit();
		}
		$this->data['data'] = array();
		$this->data['module_title'] = lang('page_create');
		$this->data['module_title_desc'] = lang('page_create_desc');

		$this->data['temp'] = 'admin/page/detail';
		$this->load->view('admin/main', $this->data);
	}

	public function update($id) {
		$this->load->helper('form');
		$this->load->library('form_validation');
		$_POST['id'] = $id;
		$page = $this->model_page->get_info($id);
		if ($page === FALSE)
			show_error(lang('invalid_page'));
		$this->data['page'] = $page;
		if ($this->input->post('submit')) {
			$this->form_validation->set_rules('title', 'lang:page_title', 'required');
			$this->form_validation->set_rules('content', 'lang:page_content', 'required');
			$this->form_validation->set_rules('catalogues', 'lang:catalogues', 'required');
			if ($this->form_is_valid()) {
				$data = request_filter(array('title', 'content', 'summary', 'image_id', 'home', 'main', 'product'));

				if ($data['image_id'] != $page->image_id) {
					$this->model_file->update($data['image_id'], array('status' => 1));
					$img = $this->model_file->get_info($data['image_id']);
					$data['image'] = $img->file_name;
					$data['thumb'] = $img->thumb_name;
				}
				if ($data['main'] == 1)
					$this->model_page->update_rule(array(), array('main' => 0));
				$this->model_page->update($page->id, $data);

				//catalogues
				$catalogues = request('catalogues');
				foreach ($page->catalogues as $cat_id)
					if (!in_array($cat_id, $catalogues))
						$this->model_page_catalogue->del_rule(array(
							'page_id' => $page->id,
							'cat_id' => $cat_id
						));
				foreach ($catalogues as $cat_id) {
					if (!in_array($cat_id, $page->catalogues))
						$this->model_page_catalogue->create(array(
							'page_id' => $page->id,
							'cat_id' => $cat_id
						));
				}

				echo json_encode(array(
					'state' => 'success',
					'redirect' => url_create('admin', 'page', 'index')
				));
			} else {
				echo json_encode(array(
					'state' => 'error',
					'errors' => $this->data['errors']
				));
			}
			exit();
		}
		$this->data['module_title'] = lang('page_create');
		$this->data['module_title_desc'] = lang('page_create_desc');

		$this->data['temp'] = 'admin/page/update';
		$this->load->view('admin/main', $this->data);
	}

	public function _check_id($id) {
		if ($this->model_page->get_total(array(
					'id' => $id
				)) < 1) {
			$this->form_validation->set_message(__FUNCTION__, lang('error'));
			return FALSE;
		}
		return TRUE;
	}

	public function _init() {
		$this->load->model('model_catalogue');
		$this->load->model('model_location');

		$catalogues = $this->model_catalogue->get_list_hash($this->siteConfig->lang, array(
			'where' => array('type' => 0),
			'order' => array('index', 'asc')
		));
		array_to_object($catalogues);

		object_prefix_create($catalogues, '', '&nbsp;&nbsp;&nbsp;&nbsp;');
		$this->data['catalogues'] = array();
		object_to_array($this->data['catalogues'], $catalogues);
	}

	public function delete() {
		$this->load->helper('form');
		$this->load->library('form_validation');
		$id = $this->input->post('id');
//		$id = request('id');
		$this->form_validation->set_rules('id', 'lang:page_id', 'required|callback__check_id');
		if ($this->form_validation->run()) {
			$page = $this->model_page->get_info($id);
			$this->model_page_catalogue->del_rule(array(
				'page_id' => $page->id,
			));
			$this->model_file->update($page->image_id, array('status' => 0));
			$this->model_page->del($id);
			echo json_encode(array(
				'state' => 'success',
				'id' => $id,
			));
		} else {
			echo json_encode(array(
				'state' => 'error',
				'title' => lang('error'),
				'message' => lang('page_not_exist')
			));
		}
	}

}

?>